package com.hw.one.collect.radar.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hw.one.collect.mapper.SiteDao;
import com.hw.one.collect.mapper.VideoAlarmDao;
import com.hw.one.collect.radar.config.RadarConfig;
import com.hw.one.collect.radar.po.RadarSitePo;
import com.hw.one.collect.radar.po.VideoAlarm;
import com.hw.one.collect.radar.util.ImageUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.math.BigDecimal;
import java.util.*;


/**
 * Created by lyk on 2017/12/18.
 */
@Service
public class VideoImageService {

    protected Logger log = LoggerFactory.getLogger(this.getClass());


    @Resource
    private SiteDao siteDao;

    @Resource
    private VideoAlarmDao videoAlarmDao;

    private static List<Map<String, Object>> monitorCodeList = new ArrayList<>();

    private static Map<String, RadarSitePo> radarSiteMap = new HashMap<>();

    public void setMonitorTypeCode() {
        Map<String, Object> codeMap = new HashMap<>();
        codeMap.put("code", "TYSPJC");
//        monitorCodeList = dictionaryDao.retrieveChildListByCode(codeMap);
    }

    public void setSiteInfo() {
        Map<String, Object> param = new HashMap<>();
        param.put("monitorCode", "1000");
        param.put("siteTypeCode", "108");
        List<RadarSitePo> radarSitePos = siteDao.selectSiteInfo(param);
        for (RadarSitePo po : radarSitePos) {
            radarSiteMap.put(po.getSNCode(), po);
        }
    }

    public void videoImage(String data) {
        // 先设置监测类型
        if (monitorCodeList == null || monitorCodeList.size() < 1) {
//            setMonitorTypeCode();
        }
        if (radarSiteMap == null || radarSiteMap.isEmpty()) {
            setSiteInfo();
        }
        // 格式化成json字符串
        JSONObject dataJson = JSON.parseObject(data);
        // 获取摄像机编号
        String cameraIndexCode = dataJson.getString("cameraIndexCode");
        // 环境污染行为类型
        String monitorType = dataJson.getString("monitorType");
        if (StringUtils.isBlank(cameraIndexCode) || StringUtils.isBlank(monitorType)) {
            return;
        }
        // 疑似概率
        BigDecimal probability = dataJson.getBigDecimal("probability");
        // 环境污染行为图片
        String imageBase64 = dataJson.getString("imageBase64");
        // 当前帧所处的时间
        String frameTime = dataJson.getString("frameTime");
        // 当前帧读取完成时的时间
        String readTime = dataJson.getString("readTime");
        // 发送该帧分析结果数据时的时间
        String sendTime = dataJson.getString("sendTime");
        // 从该帧读取完成到分析完成耗时
        BigDecimal costTime = dataJson.getBigDecimal("costTime");
        // 分析程序所在服务器的IP地址
        String serverIp = dataJson.getString("serverIp");
        // 污染行为序列号
        String seqNb = dataJson.getString("seqNb");

        // 主键id
        String uuid = UUID.randomUUID().toString().replace("-", "");

        // 保存图片
        String serverPath = RadarConfig.getInstance().get("ServerPath");
        log.info("保存路径：" + serverPath);
        String type = RadarConfig.getInstance().get("videoType");
        log.info("保存类型：" + type);
        // 图片类型
        String imageType = RadarConfig.getInstance().get("imageType");
        log.info("图片类型：" + imageType);
        log.info("报警设备：" + cameraIndexCode);
        log.info("报警类型：" + monitorType);
        log.info("疑似概率：" + probability);
        //  图片名称
        String imageName = File.separator + seqNb+ "." + imageType;
        // 图片生成时间
        String datePath = frameTime.substring(0, 10);
        // 图片路径
        String imageUrl = File.separator + type + File.separator + datePath + imageName;
        // 图片绝对路径
        String imagePath = serverPath + imageUrl;
        // 保存图片
        log.info("imagePath：" + imagePath);
        // 随机保存图片
        Random random = new Random();
        if (random.nextInt(100) == 20){
            boolean flag = ImageUtil.generateImage(imageBase64, imagePath);
        }

        VideoAlarm videoAlarm = new VideoAlarm();
        if (radarSiteMap.get(cameraIndexCode) != null) {
            videoAlarm.setSitecode(radarSiteMap.get(cameraIndexCode).getSiteCode());
        }
        videoAlarm.setSncode(cameraIndexCode);
        videoAlarm.setMonitortype(monitorType);
        videoAlarm.setProbability(probability);
        videoAlarm.setFrametime(frameTime);
        videoAlarm.setReadtime(readTime);
        videoAlarm.setSendtime(sendTime);
        videoAlarm.setCosttime(costTime);
        videoAlarm.setServerip(serverIp);
        videoAlarm.setSeqnb(seqNb);
        videoAlarm.setId(uuid);
        videoAlarm.setImageurl(imageUrl);
        videoAlarm.setCreatetime(new Date());
        // TODO 视频路径
        videoAlarm.setVideourl("/no");
        videoAlarmDao.insertSelective(videoAlarm);

    }
}
